import sun.reflect.generics.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-07-24
 * Time: 8:38
 */
public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = root;
        TreeNode newNode = new TreeNode(key);
        if(root == null){
            root = newNode;
            return true;
        }
        while (node != null){
            if(key < node.key){
                if(node.left == null) {
                    node.left = newNode;
                    return true;
                }
                else node = node.left;
            }
            else {
                if(node.right == null) {
                    node.right = newNode;
                    return true;
                }
                else node = node.right;
            }
        }
        return false;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode node = root;
        while (node != null){
            if(node.key == key){
                return node;
            }
            if(key < node.key){
                node = node.left;
            }
            else {
               node = node.right;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode node = root;
        TreeNode pre = null;
        while (node != null){
            if(node.key == key){
                break;
            }
            if(key < node.key){
                pre = node;
                node = node.left;
            }
            else {
                pre = node;
                node = node.right;
            }
        }
        if(node == null)return false;

        if(node.left == null){
            if(node == root){
                root = node.right;
                return true;
            }
            if(node == pre.left){
                pre.left = node.right;
                return true;
            }
            if(node == pre.right){
                pre.right = node.right;
                return true;
            }
        }

        if(node.right == null){
            if(node == root){
                root = node.left;
                return true;
            }
            if(node == pre.left){
                pre.left = node.left;
                return true;
            }
            if(node == pre.right){
                pre.right = node.left;
                return true;
            }
        }
        //所删除节点两边都有子节点
        TreeNode tmp = node.right;
        TreeNode tmpPre = node;
        if(tmp.left == null){
            node.key = tmp.key;
            node.right = null;
            return true;
        }
        while(tmp.left != null){
            tmpPre = tmp;
            tmp = tmp.left;
        }
        node.key = tmp.key;
        tmpPre.left = null;
        return true;
    }

    public static void main(String[] args) {

    }
}